package com.sgb.goods.viewmodel;

import androidx.databinding.ObservableBoolean;
import androidx.databinding.ObservableField;
import androidx.lifecycle.MutableLiveData;

import com.sgb.goods.api.GoodsCallBack;
import com.sgb.goods.api.GoodsNet;
import com.sgb.goods.entity.DictEntity;
import com.sgb.goods.entity.GoodsManageListEntity;
import com.sgb.goods.entity.MenuEntity1;
import com.sgb.goods.utils.Constants;
import com.sgb.goods.utils.base.viewmodel.BaseViewModel;
import com.sgb.goods.view.adapter.GoodsManageListAdapter;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品管理-商品管理列表界面(页面一)
 * @author cyj
 */
public class GoodsManageListLeftViewModel extends BaseViewModel {

    private int page = 1;
    public GoodsManageListAdapter adapter;
    /** 是否展开商品状态弹窗 */
    public ObservableBoolean isOpenGoodsStatus = new ObservableBoolean();
    /** 是否展开商品分类弹窗 */
    public ObservableBoolean isOpenGoodsType = new ObservableBoolean();
    public ObservableField<String> condition = new ObservableField<>("");
    public ObservableField<String> status = new ObservableField<>("");
    public ObservableField<String> cid3 = new ObservableField<>("");
    /** 商品类型分类列表 */
    public MutableLiveData<List<MenuEntity1<MenuEntity1<MenuEntity1>>>> goodsTypeList = new MutableLiveData<>();
    /** 商品管理列表 */
    public MutableLiveData<List<GoodsManageListEntity>> goodsList = new MutableLiveData<>();
    /** 商品状态列表 */
    public MutableLiveData<List<DictEntity>> dictEntityList = new MutableLiveData<>();

    public GoodsManageListLeftViewModel() {
        adapter = new GoodsManageListAdapter();
    }

    /** 获取商品列表 */
    public void getGoodsList(boolean isLoadMore) {
        page = isLoadMore ? page + 1 : 1;
        List<String> cid3List = null;
        if (!cid3.get().equals("")) {
            cid3List = new ArrayList<>();
            cid3List.add(cid3.get());
        }
        String companyId = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.REAL_ID,"");
        Map map = new HashMap();
        map.put("pageNum", page);
        map.put("pageSize", Constants.PAGE_SIZE_10);
        map.put("text", condition.get());
        map.put("status", status.get());
        if (cid3List != null && cid3List.size() > 0) {
            map.put("cid3", cid3List);
        }
        map.put("companyId", companyId);
        showLoading();
        GoodsNet.getInstance().getGoodsApi().myGoodsList(map).enqueue(new GoodsCallBack<BaseEntity<ListResponse<List<GoodsManageListEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<GoodsManageListEntity>>> data) {
                goodsList.setValue(data.getData().getList());
                if(isLoadMore) {
                    adapter.addData(data.getData().getList());
                } else {
                    adapter.setNewData(data.getData().getList());
                }
            }

            @Override
            public void onFail() {
                goodsList.setValue(null);
            }
        });
    }

    /** 获取商品列表全部条件 */
    public void goodsStatusCategory() {
        showLoading();
        GoodsNet.getInstance().getGoodsApi().goodsStatusCategory().enqueue(new GoodsCallBack<BaseEntity<List<DictEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<DictEntity>> data) {
                dictEntityList.setValue(data.getData());
            }
        });
    }

    /** 获取分类类别 */
    public void getGoodsCategory() {
        showLoading();
        GoodsNet.getInstance().getGoodsApi().goodsCategory().enqueue(new GoodsCallBack<BaseEntity<List<MenuEntity1<MenuEntity1<MenuEntity1>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<MenuEntity1<MenuEntity1<MenuEntity1>>>> data) {
                goodsTypeList.setValue(data.getData());
            }
        });
    }

    /** 上、下架、删除商品 */
    public void upOrDownOrDeleteGoods(String opt, String... goodsIds) {
        Map map = new HashMap();
        map.put("status", opt);
        map.put("goodsIds", goodsIds);
        showLoading();
        GoodsNet.getInstance().getGoodsApi().upDown(map).enqueue(new GoodsCallBack<BaseEntity<Object>>() {
            @Override
            public void onSuccess(BaseEntity<Object> data) {
                String msg = "";
                if (opt.equals(Constants.GOODS_OPT_UP)) {
                    msg = "上架成功";
                } else if (opt.equals(Constants.GOODS_OPT_DOWN)) {
                    msg = "下架成功";
                } else if (opt.equals(Constants.GOODS_OPT_DEL)) {
                    msg = "删除成功";
                }
                MToast.showToast(application.getApplicationContext(), msg);
                getGoodsList(false);
            }
        });
    }

}
